package com.yihong.userserver.service.impl;

import com.yihong.common.util.AjaxJson;
import com.yihong.common.util.AjaxJsonFactory;
import com.yihong.common.util.StringUtil;
import com.yihong.userserver.entity.Role;
import com.yihong.userserver.entity.User;
import com.yihong.userserver.repository.RoleRepository;
import com.yihong.userserver.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    private final Logger LOG = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleRepository roleRepository;


    @Override
    public AjaxJson listRoles(String name) {
        List <Role> roles = null;
        User user;
        try {
            if(StringUtil.isEmpty(name)){
                roles = roleRepository.findAll();
            }else{
                roles = roleRepository.findByNameLike("%"+name+"%");
            }
            for (int i = 0; i < roles.size(); i++) {
                roles.get(i).setUserList(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxJsonFactory.createAjaxJson(500,"查询失败！");
        }
        return AjaxJsonFactory.createAjaxJson(200,roles);
    }

    /**
     *  更新角色状态
     * @param id 角色id
     * @param isValid 是否有效
     * @return
     */
    @Override
    @Transactional
    public AjaxJson updateRoleForIsValid(String id, int isValid) {

        Role role = roleRepository.getOne(Long.parseLong(id));
        role.setIsValid(isValid);
        try {
            roleRepository.saveAndFlush(role);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            AjaxJsonFactory.createAjaxJson(500,"更新角色状态失败！");
        }

        return AjaxJsonFactory.createOKWithMessage("更新成功！");
    }

    @Transactional
    @Override
    public AjaxJson saveOrUpdate(Role role){
        String name = role.getName();
        Role roleData = roleRepository.findByName(name);
        Long id = role.getId();
        if(id != null){
            try {
                //获取编辑前的角色对象
                Role editBeforeRole = roleRepository.getOne(id);
                role.setCreateUserId(editBeforeRole.getCreateUserId());
                role.setCreateTime(editBeforeRole.getCreateTime());
                role.setIsValid(editBeforeRole.getIsValid());
                String editBeforeRoleName = editBeforeRole.getName();
                //若编辑前的角色名称与现在角色名称不同
                if(!name.equals(editBeforeRoleName)){
                    if (roleData != null) {
                        return AjaxJsonFactory.createAjaxJson(500, "角色名称不可重复！");
                    }
                }
                roleRepository.save(role);
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxJsonFactory.createAjaxJson(500, "编辑角色失败！");
            }
        }else{
            if (roleData != null) {
                return AjaxJsonFactory.createAjaxJson(500,"角色名称不可重复！");
            }
            try {
                roleRepository.save(role);
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxJsonFactory.createAjaxJson(500, "新增角色失败！");
            }
        }
        return AjaxJsonFactory.createOK();
    }


}
